﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using entities;
using context;

namespace DataAccessLibrary.DAL
{
    public class CheckinDAO
    {
        private BlueMDataContext _ctx = new BlueMDataContext();
        public List<CheckIn> SelectCheckInByBookingId(int bookingId)
        {
            var query = _ctx.CheckIns.Where(p => p.BookingID == bookingId);
            return query.ToList();
        }

        public bool DeleteCheckIn(int checkInId)
        {
            try
            {
                CheckIn checkIn = _ctx.CheckIns.FirstOrDefault(p => p.CheckInID == checkInId);
                if (checkIn == null)
                {
                    throw new Exception("Invalid checkin id=" + checkInId);
                }
                _ctx.CheckIns.DeleteOnSubmit(checkIn);
                return true;
            }
            catch (Exception)
            {
                _ctx = new BlueMDataContext();
                throw;
            }
        }

        public CheckIn SelectCheckIn(int checkinId)
        {
            return _ctx.CheckIns.FirstOrDefault(p => p.CheckInID == checkinId);
        }

        public List<CheckIn> SelectCheckInIncome()
        {
            IQueryable<CheckIn> query = _ctx.CheckIns.Where(p => p.CheckInDate > DateTime.Today);
            query = query.OrderByDescending(p => p.CheckInDate);
            return query.ToList();
        }
    }
}
